package usr;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import page.PageState;

public class UpdateAccount extends HttpServlet {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		HttpSession sess = req.getSession();
		Usr usr = (Usr)sess.getAttribute("usr");
		
		int id = Integer.parseInt(req.getParameter("id"));
		Usr otherUsr = new Usr(id);
		otherUsr.getUsr();
		
		if ( usr == null )
		{
			req.setAttribute("error_cause", PageState.LOGIN_NOT_EXIST);
			req.getRequestDispatcher("login.jsp").forward(req, resp);
			
		} else if ( usr.getCredit() != 0 || 
						otherUsr.getCredit() == 0 ) {
			req.setAttribute("error_cause", PageState.NOT_PERMITTED);
			req.getRequestDispatcher("dashboard.jsp").forward(req, resp);
		
		} else {
			
			int credit = Integer.parseInt(req.getParameter("credit"));
			int money = Integer.parseInt(req.getParameter("money"));
			
			otherUsr.setCredit(credit);
			otherUsr.setMoney(money);
			System.out.println(otherUsr.getPassword());
			otherUsr.modify();
			
			req.getRequestDispatcher("users.jsp").forward(req, resp);
		}
	}
}
